package com.dataseek.iot.device.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dataseek.iot.device.dto.LiquidJarDto;
import com.dataseek.iot.device.entity.DeviceAttrValue;
import com.dataseek.iot.device.entity.LiquidJar;
import com.dataseek.iot.device.mapper.DeviceAttValueMapper;
import com.dataseek.iot.device.service.LiquidJarService;
import com.dataseek.iot.device.vo.LiquidJarShowVo;
import com.dataseek.iot.device.vo.LiquidJarVo;
import com.dataseek.iot.toolikit.common.enums.RspStatusEnum;
import com.dataseek.iot.toolikit.common.response.ResponseResult;
import com.dataseek.iot.toolkit.mybatis.entity.DeviceList;
import com.dataseek.iot.toolkit.mybatis.mapper.DeviceListMapper;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RestController
@CrossOrigin
@Slf4j
@RequestMapping("**/liquidJar")
public class LiquidJarController {

    @Autowired
    private LiquidJarService liquidJarService;

    @Autowired
    private DeviceListMapper deviceListMapper;

    @Autowired
    private DeviceAttValueMapper deviceAttValueMapper;

    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ResponseResult query(@RequestParam(value="page")  Integer page,
                                      @RequestParam(value="size")  Integer size,
                                      @RequestParam(value="jarName", required=false) String jarName,
                                      @RequestParam(value="lineCode", required=false) String lineCode) throws IOException {
        LiquidJarVo tenantVo = liquidJarService.queryAction(jarName, lineCode, page,size);
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),"成功",tenantVo);
    }

    @RequestMapping(value = "/qryJarByCode", method = RequestMethod.GET)
    public ResponseResult qryJarByCode(@RequestParam(value="jarCode", required=true) String jarCode) throws IOException {
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),"成功",liquidJarService.qryJarByCode(jarCode));
    }

    @RequestMapping(value = "/qryJarByDeviceCode", method = RequestMethod.GET)
    public ResponseResult qryJarByDeviceCode(@RequestParam(value="deviceCode", required=true) String deviceCode) throws IOException {
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),"成功",liquidJarService.qryJarByDeviceCode(deviceCode));
    }


    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public ResponseResult insert(@RequestBody LiquidJar liquidJar) throws IOException {
        Integer code  = liquidJarService.insertAction(liquidJar);
        ResponseResult responseResult = null;
        if (code == RspStatusEnum.FAIL.getCode()) {
            responseResult=new ResponseResult(RspStatusEnum.FAIL.getCode(),"储液罐编码或名称重复，请重新输入!",code.toString());
        } else {
            responseResult=new ResponseResult(RspStatusEnum.SUCCESS.getCode(),"成功!",code.toString());
        }
        return responseResult;
    }


    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResponseResult update(@RequestBody LiquidJar liquidJar) throws IOException {
        Integer code  = liquidJarService.updateAction(liquidJar);
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),RspStatusEnum.SUCCESS.getMessage(),code.toString());
    }


    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ResponseResult delete(@RequestBody JSONObject ids)  throws IOException {
        JSONArray jsonArray = ids.getJSONArray("ids");
        List<Integer> listIds = JSONObject.parseArray(jsonArray.toJSONString(), Integer.class);
        Integer code  = liquidJarService.deleteAction(listIds);
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),RspStatusEnum.SUCCESS.getMessage(),code.toString());
    }

    @RequestMapping(value = "/queryJarShowList", method = RequestMethod.GET)
    public ResponseResult queryJarShowList(@RequestParam(value="page")  Integer page,
                                           @RequestParam(value="size")  Integer size,
                                           LiquidJarDto qryDto) {
        LiquidJarShowVo tenantVo = liquidJarService.queryJarShowList(qryDto,page,size);
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),"成功",tenantVo);
    }


    //设备总数
    @RequestMapping(value = "/qryDeviceCnt", method = RequestMethod.GET)
    public ResponseResult qryDeviceCnt() {
        QueryWrapper<DeviceList> qw = new QueryWrapper<>();
        int cnt = deviceListMapper.selectCount(qw);
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),"成功", cnt);
    }

    //在线设备数量
    @RequestMapping(value = "/qryActiveDeviceCnt", method = RequestMethod.GET)
    public ResponseResult qryActiveDeviceCnt() {
        QueryWrapper<DeviceList> qw = new QueryWrapper<>();
        qw.eq("status", "active");
        int cnt = deviceListMapper.selectCount(qw);
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),"成功", cnt);
    }

    //报警储液罐数量
    @RequestMapping(value = "/qryAlarmCnt", method = RequestMethod.GET)
    public ResponseResult qryAlarmCnt() {
        QueryWrapper<DeviceList> qw = new QueryWrapper<>();
        qw.in("alarm_status", Arrays.asList(1,2,3,4));
        int cnt = deviceListMapper.selectCount(qw);
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),"成功", cnt);
    }

    //待加液储液罐数量
    @RequestMapping(value = "/qryLowCnt", method = RequestMethod.GET)
    public ResponseResult qryLowCnt() {
        QueryWrapper<DeviceList> qw = new QueryWrapper<>();
        List<DeviceList> deviceLists = deviceListMapper.selectList(qw);
        int cnt = 0;
        QueryWrapper<DeviceAttrValue> qwAttr = new QueryWrapper<>();
        for (DeviceList tmpDevice : deviceLists) {
            qwAttr.eq("device_code", tmpDevice.getDeviceCode());
            float curLevel = 0;
            float lowLevel = 0;
            List<DeviceAttrValue> attrValueList = deviceAttValueMapper.selectList(qwAttr);
            for (DeviceAttrValue attrValue : attrValueList) {
                if ("liquidLevel".equals(attrValue.getAttCode())) {
                    curLevel = attrValue.getAttValue();
                }
                if ("thresLow".equals(attrValue.getAttCode())) {
                    lowLevel = attrValue.getAttValue();
                }
            }
            //当前液位小于低限值就计数
            if (curLevel < lowLevel) {
                cnt++;
            }
        }
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),"成功", cnt);
    }

}
